import axios from 'axios'
import { ElNotification} from "element-plus";
import '@/mock/course';
import '@/mock/user';
import "@/mock/author";

const service = axios.create({
    baseURL: "/mock",
    timeout: 20000
})

/**
 * 请求拦截器
 */
service.interceptors.request.use(function (config) {
    // TODO 添加token
    config.headers['token'] = 'token';

    // 某些接口去除token
    if (config.params && config.params.noToken) {
        config.headers['token'] = '';
    }

    return config;
}, function (error) {
    console.log(error);
    return Promise.reject(error);
})

/**
 * 响应拦截器
 */
service.interceptors.response.use(function (response) {
    const data = response.data;
    if (data.code !== 20000) {
        ElNotification({
            title: '请求错误',
            message: '错误代码：' + data.code + '<br/>错误路径：' + response.request.custom.url + '<br/>错误信息：' + data.message,
            type: 'error',
            dangerouslyUseHTMLString: true,
            position: 'bottom-left'
        })
    }

    return data.data;
}, function (error) {
    console.log(error)
    ElNotification({
        title: '请求错误',
        message: '错误代码：' + error.request.status + '<br/>错误路径：' + error.request.responseURL + '<br/>错误信息：' + error.message,
        type: 'error',
        dangerouslyUseHTMLString: true,
        position: 'bottom-left'
    })
    return Promise.reject(error);
})

export default service;